package com.lwc.service.impl;

import com.alibaba.excel.EasyExcel;
import com.lwc.bean.AccountDetail;
import com.lwc.bean.Enterprise;
import com.lwc.bean.EnterpriseData;
import com.lwc.bean.Xufei;
import com.lwc.common.crud.BaseServiceImpl;
import com.lwc.common.exceotion.BusinessexCeption;
import com.lwc.mapper.AccountDetailMapper;
import com.lwc.mapper.EnterpriseCategoryMapper;
import com.lwc.mapper.EnterpriseMapper;
import com.lwc.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class EnterpriseServiceImpl extends BaseServiceImpl<Enterprise, EnterpriseMapper> implements EnterpriseService {

    private  String currentMaxCode;

    @Autowired
    EnterpriseCategoryMapper enterpriseCategoryMapper;

    @Autowired
    HttpServletRequest request;

    @Transactional
    @Override
    public int save(Enterprise entity) {
        //检查用户名是否可用
        int count= baseMapper.existName(entity.getName());
        if (count>0)
          //抛出异常
throw  new BusinessexCeption("企业名名称不能重复");
        entity.setCode(generateNewCode());
//保存企业
        int rs=super.save(entity);
        if (rs==0){
            throw  new BusinessexCeption("保存企业失败");
        }
        //更新类别数量
        rs=enterpriseCategoryMapper.addEnterpriseNum(entity.getCategoryCode());
        return rs;
    }
    //如果可以用自动声明编码
    String generateNewCode(){
if (null!=currentMaxCode){
  return getNextCode(currentMaxCode);
}
//内存没有 查询最大编码
currentMaxCode= baseMapper.queryCode();
if (currentMaxCode==null){
    //初始值
    int tem=1000001;
 String MaxCode=tem+"";
    return MaxCode;
}
//如果存在
return getNextCode(currentMaxCode);
    }

String getNextCode(String code){
    //如果内容存在最大编码    转化成数字类型
    int MaxCode=Integer.parseInt(code);
    MaxCode++;
    String currentMaxCode=MaxCode+"";
    return currentMaxCode;
}

    @Override
    public int auth(int id, int empNum, int accountNum) {
        return baseMapper.auth(id, empNum, accountNum);
    }


    @Autowired
    AccountDetailMapper accountDetailMapper;
    @Override
    public int xufei(Xufei xufei) {
    int rs= baseMapper.xufei(xufei);
    if (rs==0){
        throw new BusinessexCeption("账户变更错误");
    }
if (xufei.getMonth()>0){
   //如果有增加会员日期    判断是否是首次购买 如果是 更新开通日期
    baseMapper.updateBeginDate(xufei.getEid());
}
    //更新成功了 更新账户数据
        AccountDetail detail=new AccountDetail();
    detail.setType(2);
      detail.setEid(xufei.getEid());
detail.setMoney(xufei.getMoney());
detail.setReason("企业续费"+xufei.getMonth()+"个月");
rs= accountDetailMapper.save(detail);
return rs;
    }

    @Override
    public Map xinxi(int id) {
return  baseMapper.xinxi(id);
    }

    @Override
    public String export(Map map) {
        //返回导出的数据
        List<EnterpriseData>list= baseMapper.export(map);
        //获取服务器的根目录
        String root=request.getServletContext().getRealPath("/")+"/excel";
//创建文件
        File file=new File(root);
        if (!file.exists())
            //如果文件不存在就创建
            file.mkdirs();
        String newFileName=file.getAbsolutePath()+"/企业列表"+".xlsx";
        EasyExcel.write(newFileName, EnterpriseData.class)
                .sheet("模板")
                .doWrite(() -> {
                    return list;
                });
        return "/excel/企业列表.xlsx";
    }
}
